package demo2;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:装牌的盒子
 * User: LianBao
 * Date: 2024-03-22
 * Time: 14:23
 */
public class Box {

    //4个花色 每个花色13张牌
    public static final String[] flowers = {"♥", "♠", "♣", "♦"};

    //生成一副牌
    public List<Card> BuyCard() {
        List<Card> cardList = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            for (int j = 1; j <= 13; j++) {
                int Number = j;
                String Flower = flowers[i];
                Card card = new Card(Flower, Number);
                cardList.add(card);
            }
        }
        return cardList;
    }

    //洗牌
    public void wash(List<Card> cardList) {
        Random random = new Random();
        for (int i = 51; i > 0; i--) {
            int x = random.nextInt(i);
            swap(cardList, i, x);
        }


    }

    //交换
    private void swap(List<Card> cardList, int i, int j) {
        Card tmp = cardList.get(i);
        cardList.set(i, cardList.get(j));
        cardList.set(j, tmp);
    }


    //抓牌
    public void game(List<Card> cardList) {
        //3个人,轮流抓5张牌
        List<Card> box1 = new ArrayList<>();
        List<Card> box2 = new ArrayList<>();
        List<Card> box3 = new ArrayList<>();
        List<List<Card>> gamers = new ArrayList<>();
        gamers.add(box1);
        gamers.add(box2);
        gamers.add(box3);


        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                //从0下标移除出元素,用tmp保存(相当于每次从第一张牌开始抓)
                Card tmp = cardList.remove(0);
                //将抓的牌添加到每个人手里
                gamers.get(j).add(tmp);
            }
        }

        System.out.println("一号玩家" + gamers.get(0));
        System.out.println("二号玩家" + gamers.get(1));
        System.out.println("三号玩家" + gamers.get(2));
        System.out.println("剩余牌" + cardList);
    }

}
